package com.heoller.service.strategy;

import com.heoller.dto.CustomParam;
import com.heoller.service.generator.postprocess.AbstractGeneratorPostProcessService;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.zip.ZipOutputStream;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author huliang
 * @date 2021-11-25 16:06
 */
@Component
public class GeneratorStrategy {

    private static final String BEAN_NAME_SUFFIX = "PostProcessService";

    @Autowired
    private List<AbstractGeneratorPostProcessService> generatorPostProcessServiceList;

    private Map<String, AbstractGeneratorPostProcessService> generatorPostProcessServiceMap;

    @PostConstruct
    public void init() {
        generatorPostProcessServiceMap = generatorPostProcessServiceList.stream().collect(
            Collectors.toMap(AbstractGeneratorPostProcessService::getBeanName, service -> service));
    }

    /**
     * 后续处理
     * @param module
     */
    public void postProcess(ZipOutputStream zip, String module, CustomParam customParam) {
        AbstractGeneratorPostProcessService postProcessService = generatorPostProcessServiceMap
            .get(module + BEAN_NAME_SUFFIX);
        if (Objects.nonNull(postProcessService)) {
            postProcessService.doPostProcess(zip, module, customParam);
        }
    }

}
